package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewCourseDetail;
import vn.student.mylib.Constants;
import vn.student.mylib.Course;
import vn.student.mylib.ListCourseAdapter;
import vn.student.mylib.Semester;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class ViewSemesterDetailController {
	Activity act = null;
	String id;
	AStudentDB myDB = null;
	public ViewSemesterDetailController(Activity act,String id){
		this.act = act;
		this.id = id;
		myDB = new AStudentDB(act);
	}
	public void showSemesterDetail(String id){
		Semester semester = myDB.getSemesterDetail(id);
		TextView tmpLabel = (TextView) act.findViewById(R.id.semester_name);
		tmpLabel.setText(semester.semesterName);
		tmpLabel = (TextView) act.findViewById(R.id.dateStart);
		tmpLabel.setText("From " + semester.dateStart +" to " + semester.dateEnd);
		showCourseList();
		
	}
	public void showCourseList(){
		ArrayList<Course> listCourse = myDB.getCourseList(id);
		if(listCourse==null||listCourse.size()==0)
			return;
		ListView listCourseView = (ListView)act.findViewById(R.id.listCourseView);
		listCourseView.setAdapter(new ListCourseAdapter(act,listCourse));
		listCourseView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewCourseDetail.class);
				Bundle bundle = new Bundle();
				ListCourseAdapter adapter = (ListCourseAdapter) arg0.getAdapter();
				ArrayList<Course> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewCourseDetailRequestCode);
			}
		});
		listCourseView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.add(0,Constants.SEMESTER_DETAIL_CONTEXTMENU_ADD_COURSE,0, "Add new");
				menu.add(0,Constants.SEMESTER_DETAIL_CONTEXTMENU_EDIT_COURSE,1, "Edit");
				menu.add(0,Constants.SEMESTER_DETAIL_CONTEXTMENU_DELETE_COURSE,2, "Delete");
			}
       }); 
	}
	public boolean removeCourse(String id) {
		return myDB.removeCourse(id);
	}
}
